package com.stockz.ui.swing.charting.widget;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import org.springframework.core.Ordered;

import com.stockz.core.model.Bar;
import com.stockz.core.util.Global;
import com.stockz.ui.swing.charting.display.Displayable;
import com.stockz.ui.swing.charting.util.RepaintStockChartPanelUtil;
import com.stockz.ui.swing.charting.util.UIChartingConstant;
import com.stockz.ui.swing.charting.view.StockChartPanel;
import com.stockz.ui.swing.charting.view.StockChartPanelState;
import com.stockz.ui.swing.widget.JMenuItem;

public class ClipMenuItem extends JMenuItem implements Ordered {
	private static final long serialVersionUID = 646307461526391359L;
	
	public ClipMenuItem(){
		super("Clip");
		addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				StockChartPanel panel = (StockChartPanel)Global.get(UIChartingConstant.SelectedStockChartPanel);
				if(panel != null){
					StockChartPanelState state =panel.getStockChartPanelState();				
					StockChartRightClickPopupMenu parent = (StockChartRightClickPopupMenu)getParent();
					List<Bar> sublist = state.getBarList().subList(parent.getPoint().getIndex(),state.getBarList().size()-1);
					state.setBarList(sublist);
					for(Displayable indicatorDisplay : state.getDisplayableSet()){
						indicatorDisplay.refreshValues();
					}
					RepaintStockChartPanelUtil.repaintStockChartPanel(panel);
				}
				
			}
		});
	}
	
	@Override
	public int getOrder() {
		return 1;
	}

}
